﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DotNet.Business;
using DotNet.Model;
using DotNet.Common;
using System.Text;
public partial class Admin_adminProductClass : basePage
{
    productClassBll pcBll = new productClassBll();
    private static readonly object objSync = new object();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            #region 页面第一次加载
            if (Request["action"] != null)
            {
                
                string action = Request.QueryString["action"].ToString();
                string id = Request["id"].ToString();
                switch (action.Trim())
                {
                    case "delAll":
                        delAll(id);
                        break;
                    case "editAll":
                        editAll(id);
                        break;
                    case "Insert":
                        Insert(id);
                        break;
                    case "MoveAll":
                        moveUpdate(id);
                        break;
                    default:
                        // instance = accInstance;
                        break;
                }
            }
            ViewState["dt"] = pcBll.GetDataTable();
            #endregion
        
        }
        else
        {
            #region 页面回发
           
            #endregion
        }


    }
    #region  代码
    #region 显示最新的数据
    public string bindProductClassList(int ParentID, int length, StringBuilder sb)
    {
        
        DataView dvTree = pcBll.GetDataView(ViewState["dt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            sb.Append("<table class='tb2'><tr align='center'>");
            sb.Append("<td  align='left' width='20%'>" + str + "<input type='checkbox' name='m_id' value='" + row["pClassId"].ToString() + "'  class='checkbox'  id='" + row["pClassId"].ToString() + "' />" + row["pClassId"].ToString() + ".<a href='../zh/" + UrlTrans.TransUrl("productClass", row["pClassId"].ToString(), "", "", "", "", "") + "'>" + row["pClassName"].ToString() + "</a>(<font color='red'>" + countByClass(row["pClassId"].ToString()) + "</font>)<span id=\"state1\"><a title='" + row["pClassId"].ToString() + "' href=\"#msg\" onclick=\"GivePower(this,this.title)\"><img src=\"imgs/icon_01.gif\"   style=\"cursor:pointer\"  /></a></span></td>");
            sb.Append("<td width='14%'><input size='13' type='text' name='pcname" + row["pClassId"].ToString() + "' value='" + row["pClassName"].ToString() + "' ></td>");
            sb.Append("<td width='10%'><input size='10' type='text' name='pcTag" + row["pClassId"].ToString() + "' value='" + row["pTag"].ToString() + "'></td>");
            sb.Append("<td width='16%'><input size='12' type='text' name='pcinfo" + row["pClassId"].ToString() + "' value='" + row["pCInfo"].ToString() + "'></td>");
            sb.Append("<td width='6%'><input size='3' type='text' name='pcsort" + row["pClassId"].ToString() + "' value='" + row["cSort"].ToString() + "'></td>");
            sb.Append("<input  type='hidden' name='pFid" + row["pClassId"].ToString() + "'  value='" + row["pFid"].ToString() + "'/>");
            sb.Append("<td width='13%'><input id=\"Button6\" type=\"button\" value=\"额外信息\" class=\"btn\" title=\"" + row["pClassId"] + "\" onclick=\"javascript:location.href='addClassAttribute.aspx?action=load&pClassID='+this.title\"/>  <input type='button' value='删除' onClick=\"if(confirm('确定要删除吗')){$('#loading').show();ajax_get('?action=delAll&id='+this.title,'',function(msg){$('#cpcontainer').empty().append(msg);$('#loading').hide();});}else{return false}\" name='Input' class='btn' title='" + row["pClassId"].ToString() + "'/> </td>");
            sb.Append("</tr></table>");
            bindProductClassList(Convert.ToInt32(row["pClassId"].ToString()), length, sb);
        }
        return sb.ToString();

    }
    #endregion

    #region "AJAX批量修改,"
    public void editAll(string id)
    {
        try
        {
            
            if (id.Contains(","))
            {
                lock (objSync)
                {
                    string[] idParam = id.Split(',');
                    for (int i = 0; i <= idParam.Length - 1; i++)
                    {
                        productClass pc = new productClass();
                        pc.pClassId = Convert.ToInt32(idParam[i]);
                        pc.pClassName = Request.Form["pcname" + idParam[i] + ""].ToString();
                        pc.pFid = Convert.ToInt32(Request.Form["pFid" + idParam[i] + ""]);
                        pc.pTag = Request.Form["pcTag" + idParam[i] + ""].ToString();
                        pc.pCInfo = Request.Form["pcinfo" + idParam[i] + ""].ToString();
                        if (!CommUtil.IsNumStr(Request.Form["pcsort" + idParam[i] + ""].ToString().Trim()) || Request.Form["pcsort" + idParam[i] + ""].ToString().Trim() == "")
                        {
                            return;
                        }
                        pc.cSort = Convert.ToInt32(Request.Form["pcsort" + idParam[i] + ""].ToString());
                        if (pcBll.Update(pc))
                        {


                        }
                        else
                            return;
                    }
                }
            
            }
            else
            {
                productClass pc = new productClass();
                pc.pClassId = Convert.ToInt32(id);
                pc.pClassName = Request.Form["pcname" + id + ""].ToString();
                pc.pFid = Convert.ToInt32(Request.Form["pFid" + id + ""]); ;
                pc.pTag = Request.Form["pcTag" + id + ""].ToString();
                pc.pCInfo = Request.Form["pcinfo" + id + ""].ToString();
                if (!CommUtil.IsNumStr(Request.Form["pcsort" + id + ""].ToString()) || Request.Form["pcsort" + id + ""].ToString()=="")
                {
                    return;
                }
                pc.cSort = Convert.ToInt32(Request.Form["pcsort" + id + ""].ToString());
                if (pcBll.Update(pc))
                {
                    
                }
                else
                    return;
            }
            //Response.Write("<div style='color=green;text-align:center;font-weight:bold;font-size:12px;position:abs;z-index:100;'>更新成功</div>");
        }
        catch (Exception ex)
        {
            JsUtil.ShowMsg(ex.Message);
        }
        finally
        {
            
        }

    }
    #endregion

    #region AJAX批量删除,SQL拼接需要过滤的地方
    public void delAll(string id)
    {
        if (id.Contains(","))
        {
            string[] idParam = id.Split(',');
            string[] pClassid = new string[idParam.Length];
            for (int i = 0; i < idParam.Length; i++)
            {
                if (pcBll.ExistsChildrens(idParam[i]))
                {
                    Response.Write("<font color='red'>有子类的不能删除，请先删除子类！</font>");
                    return;
                }
                pClassid[i] = idParam[i];
            }
            if (pcBll.Delete(pClassid))
            {
               
            }
        }
        else
        {
            if (pcBll.ExistsChildrens(id))
            {
                Response.Write("<font color='red'>有子类的不能删除，请先删除子类！</font>");
                return;
            }
            if (pcBll.Delete(Convert.ToInt32(id)))
            {
               
            }
        }

    }
    #endregion

    #region AJAX添加
    public void Insert(string id)
    {
        if (Request.Form["pcname"] != null)
        {
            productClass pc = new productClass();
            if (!CommUtil.IsNumStr(Request.Form["pcsort"].ToString().Trim()) || Request.Form["pcsort"].ToString().Trim() == "")
            {
                return;
            }
            pc.cSort = Convert.ToInt32(Request.Form["pcsort"].ToString());
            pc.pCInfo = Request.Form["pCInfo"].ToString();
            pc.pClassName = Request.Form["pcname"].ToString();
            pc.pTag = Request.Form["pcTag"].ToString();
            pc.pFid = Convert.ToInt32(id);
            if (pcBll.Insert(pc))
            {

            }
            else
                return;
        }
    }
    #endregion

    #region AJAX批量移动 SQL拼接需要过滤的地方
    public void moveUpdate(string id)
    {
        try
        {
            string pcid = Request.QueryString["pcid"].ToString();
            if (pcid.Contains(","))
            {
                
                string[] idParam = pcid.Split(',');
                string[] pClassid = new string[idParam.Length];
                for (int i = 0; i <= idParam.Length - 1; i++)
                {
                    
                    pClassid[i] = idParam[i];
                }
                if (pcBll.Update(pClassid, id))
                { 
                }
            }
            else
            {
                string[] cid ={pcid};
                if (pcBll.Update(cid, id))
                {
                }
            }
        }
        catch
        {
 
        }
    }
    #endregion

    #region 统计类别下面的产品
    public string countByClass(object id)
    {
        if (id != null)
        {
            int i = Convert.ToInt32(id);
            productBll pBll = new productBll();
            return pBll.countByClass(i).ToString();
        }
        else
        {
            return null;
        }



    }
    #endregion

    #region "抛弃服务器控件,自己绑定下拉框"
    public string ProductClassDp(string name)
    {
        StringBuilder str = new StringBuilder();
        str.Append("<select name='selectDp' id='" + name + "' style='width:192px;'>");
        str.Append("<option value='0'>---顶级分类---</option>");
        str.Append(AddItems(0, 0, new StringBuilder()));
        str.Append("</select>");
        return str.ToString();
    }

    public string AddItems(int ParentID, int length, StringBuilder sb)
    {
        DataView dvTree = pcBll.GetDataView(ViewState["dt"] as DataTable, ParentID);
        length++;
        foreach (DataRowView row in dvTree)
        {
            string str = addtab(length);
            sb.AppendFormat("<option value='" + row["pClassId"].ToString() + "'>{0}</option>", str + "|--" + row["pClassName"].ToString());
            AddItems(Convert.ToInt32(row["pClassId"].ToString()), length, sb);
        }
        return sb.ToString();
    }
    private string addtab(int i)
    {
        string mark = "&nbsp;";
        if (i != 1)
        {
            for (int j = 0; j < i - 1; j++)
            {

                mark += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
        }
        return mark;
    }
    #endregion
    #endregion
}
